package component.hide
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	public class FileReference2	extends EventDispatcher
	{
		private var m_file:FileReference;
		
		private var isOpen : Boolean;
		
		private var _completeCallBack : Function;
		
		public function FileReference2()
		{
			
		}
		
		public function save(data : *,fileName : String,callBack : Function = null) : void
		{
			if(!data) return;
			
			_completeCallBack = callBack;
			
			m_file = new FileReference;
			m_file.save(data,fileName);
			
			configureListeners(m_file);
		}
		
		public function open(callBack : Function = null) : void
		{
			_completeCallBack = callBack;
			isOpen = true;
			
			m_file = new FileReference;
			m_file.browse(getTypes());
			
			configureListeners(m_file);
		}
		
		public function openByURL(url : String,callBack : Function = null):void
		{
			_completeCallBack = callBack;
		}
		
		private function OnLoadMapComplete(evt : Event):void
		{
			_completeCallBack = null;
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.CANCEL, cancelHandler);
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(Event.SELECT, selectHandler);
			dispatcher.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
		}
		
		public function destory() : void
		{
			m_file.removeEventListener(Event.CANCEL, cancelHandler);
			m_file.removeEventListener(Event.COMPLETE, completeHandler);
			m_file.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			m_file.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			m_file.removeEventListener(Event.OPEN, openHandler);
			m_file.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
			m_file.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			m_file.removeEventListener(Event.SELECT, selectHandler);
			m_file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,uploadCompleteDataHandler);
			
			m_file = null;
			_completeCallBack = null;
		}
		
		private function getTypes():Array 
		{
			var allTypes:Array = new Array(getTextTypeFilter(),getImageTypeFilter());
			return allTypes;
		}
		
		
		private function getImageTypeFilter():FileFilter
		{
			return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
		}
		
		private function getTextTypeFilter():FileFilter {
			return new FileFilter("Text Files (*.xml,*.txt, *.rtf)", "*.xml;*.txt;*.rtf");
		}
		
		private function cancelHandler(event:Event):void
		{
//			trace("cancelHandler: " + event);
		}
		
		private function completeHandler(event:Event):void
		{
//			trace("completeHandler: " + event);
			
			if(isOpen)
			{
				isOpen = false;
				this.dispatchEvent(event);
			}
			
			if(_completeCallBack!= null)
			{
				_completeCallBack();
				_completeCallBack = null;
			}
			
			destory();
		}
		
		private function uploadCompleteDataHandler(event:DataEvent):void
		{
//			trace("uploadCompleteData: " + event);
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void
		{
//			trace("httpStatusHandler: " + event);
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
//			trace("ioErrorHandler: " + event);
		}
		
		private function openHandler(event:Event):void
		{
//			trace("openHandler: " + event);
			
		}
		
		private function progressHandler(event:ProgressEvent):void
		{
//			trace("progressHandler name=" + m_file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
			this.dispatchEvent(event);
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
//			trace("securityErrorHandler: " + event);
		}
		
		private function selectHandler(event:Event):void
		{
			if(isOpen)
			{
				m_file.load();
			}
		}
		
		public function get file() : FileReference
		{
			return m_file;
		}
		
		public function get data() : *
		{
			return m_file.data;	
		}
	}
}